package main

import (
	"fmt"
	"net/http"
	"time"
)

// server 设置超时时间
// curl localhost:8090/hello
func hello2(w http.ResponseWriter, req *http.Request) {
	fmt.Println("server: hello handler started")
	defer fmt.Println("server:hello handler ended")

	time.Sleep(time.Second * 3)
	fmt.Fprintf(w, "hello \n")
}

func main() {
	// 跟前面一样，我们在 /hello 路由上注册 handler，然后开始提供服务。
	http.HandleFunc("/hello", hello2)
	server := http.Server{
		Addr:    ":8090",
		Handler: http.TimeoutHandler(http.DefaultServeMux, time.Second*2, "服务端操作超时"),
	}
	fmt.Println(`Please visit:  
    "http://localhost:8090/hello"
    `)
	//err := http.ListenAndServe(":8090", nil)
	err := server.ListenAndServe()
	if err != nil {
		panic(err)
	}
}
